
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>FreeMarker Index</title>

    <link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/style.css"/>

	<script>
		function lessNum(obj)
		{
			if(parseInt(obj.parentElement.children[2].innerHTML)<=0)
			{
				obj.parentElement.children[2].innerHTML=0;
				alert("商品数量为0，不能再减了");
			}
			else
			{
				obj.parentElement.children[2].innerHTML=parseInt(obj.parentElement.children[2].innerHTML)-1;
				var xhr = new XMLHttpRequest();
				xhr.open('post','/changeNum');
			 	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			 	xhr.send("id="+obj.parentElement.children[0].innerHTML+"&amount="+obj.parentElement.children[2].innerHTML);
			}
		}
		
		function moreNum(obj)
		{
			obj.parentElement.children[2].innerHTML=parseInt(obj.parentElement.children[2].innerHTML)+1;
			var xhr = new XMLHttpRequest();
			xhr.open('post','/changeNum');
		 	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		 	xhr.send("id="+obj.parentElement.children[0].innerHTML+"&amount="+obj.parentElement.children[2].innerHTML);
		}
		

	function buyAll(){
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			 if(xhr.readyState == 4){
	                var status = xhr.status;
	                if(status >= 200 && status < 300 || status == 304){
	                	var result = xhr.responseText;
	                	if(result == 'true'){
	                		window.location.href="/buyer/index/all";
	                		alert("购买成功");
	                		
	                	}else if(result == 'false'){
	                		alert("购买失败");
	                	}
	                	else 
	                	{
	                		alert(result+"商品存货不足");
	                	}
	                }else{
	                	alert("购买失败");
	                }
	            }
		};
		 xhr.open('post','/buy');
		 xhr.send(null);
		
	};
	</script>

  </head>

  <body>


    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>

 		<div class="navbar-brand" Left="400px">买家您好，${sys_user_key.account}</div>
	          <a class="navbar-brand" href="/system/logout">[退出]</a>
        </div>
       <div id="navbar" class="navbar-collapse collapse">
          	<form class="navbar-form navbar-right">
			<a class="navbar-brand" href="/buyer/index/all">首页</a>
			
            <a class="navbar-brand" href="/buyer/account">账务</a>
            
           	<a class="navbar-brand" href="/buyer/shoppingcart">购物车</a>
 			</form>
       </div><!--/.navbar-collapse -->
          
        
      </div>
    </nav>
  <div class="g-doc">
    <div class="m-tab m-tab-fw m-tab-simple f-cb" >
        <h2>已添加到购物车的内容</h2>
    </div>

 <div style="position:relative;line-height:35px;margin:30px 30px 0 0;" >
        <h2>  </h2>
    </div>
  <table id="newTable" class="m-table m-table-row n-table g-b3">
 	<tbody>
	 	<tr>
		 	<th>内容名称</th>
		 	<th>数量</th>
		 	<th>价格</th>
	 	</tr>
	 	
	 <#list shoppingcart_commodity as commodity>
	 	<tr>
	 	
	 		<td onclick="window.location.href='/buyer/detail?id=${commodity.commodityId}'">${commodity.title}</td>
		 	<td>
		 		<span hidden="true">${commodity.id}</span>
			 	<span onclick="lessNum(this)" class="lessNum">-</span>
			 	<span class="totalNum">${commodity.amount}</span>
			 	<span >${commodity.commodity_id}</span>
			 	<span onclick="moreNum(this)" class="moreNum">+</span>
		 	</td>
	 		<td>${commodity.price}</td>
	 	</tr>
	 </#list> 
 	</tbody>
 </table>
 	<div id="act-btn">
 	<button class="u-btn u-btn-primary"  onclick="window.location.href='/buyer/index/all'">退出</button>
 	<button class="u-btn u-btn-primary" onclick="buyAll()">购买</button></div>
</div>
</div>


<div class="n-foot">
 <#include "/footer.ftl">
</div>
    <!-- Bootstrap core JavaScript -->
    <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
    <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  </body>
</html>